package co.gov.medellin.sdep.sisdep.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.jdbc.object.SqlUpdate;

import co.gov.medellin.sdep.sisdep.model.LocalInventario;

public class LocalInventarioDAOImpl extends JdbcDaoSupport implements ILocalInventarioDAO {

    private InsertOperation insertOperation;

    private UpdateOperation updateOperation;

    private DeleteOperation deleteOperation;

    private DeleteByPrimaryKey deleteByPrimaryKey;

    private FindByPrimaryKeyQuery findByPrimaryKeyQuery;

    private FindByCriteriaQuery findByCriteriaQuery;

    private FindAllQuery findAllQuery;

    private FindByActiveStateQuery findByActiveStateQuery;

	private static final String SELECT = "SELECT * FROM CCO_LOCAL_INVENTARIO_T ";
	private static final String WHERE = " WHERE ";
	private static final String PARAMETER = " ?, ";

    public void delete(LocalInventario localInventario) {
		deleteOperation.execute( localInventario  );
	}

	public void deleteByPrimaryKey(long id) {
		deleteByPrimaryKey.execute(id);
	}

	public LocalInventario findByPrimaryKey(long login) {
		return findByPrimaryKeyQuery.runQuery(login);
	}

	public void insert(LocalInventario localInventario) {
		insertOperation.execute( localInventario );
	}

	public void update(LocalInventario localInventario) {
		updateOperation.execute( localInventario );
	}

	public LocalInventario findByCriteria(LocalInventario  localInventario) {
		return findByCriteriaQuery.runQuery(localInventario);
	}

	public List load(LocalInventario localInventario) {
		return findAllQuery.runQuery(localInventario);
	}

	public LocalInventario findByActiveState(LocalInventario  localInventario) {
		return findByActiveStateQuery.runQuery(localInventario);
	}

	public void initDao() {
    	insertOperation   = new InsertOperation( getDataSource() );
    	updateOperation = new UpdateOperation(  getDataSource() );
    	//deleteOperation = new DeleteOperation(  getDataSource() );
    	//deleteByPrimaryKey = new DeleteByPrimaryKey( getDataSource());
    	//findByPrimaryKeyQuery   = new FindByPrimaryKeyQuery(  getDataSource() );
    	//findByCriteriaQuery = new FindByCriteriaQuery(  getDataSource() );
    	findAllQuery = new FindAllQuery( getDataSource() );
    	//findByActiveStateQuery = new FindByActiveStateQuery( getDataSource() );
    }

    // ------------- SaveOperation ---------------------------------------
	class InsertOperation extends SqlUpdate	{
		private static final String sql = "INSERT INTO CCO_LOCAL_INVENTARIO_T ("
				  + " ID, "
				  + " ID_LOCAL, "
				  + " AGUA, "
				  + " ENERGIA, "
				  + " TELEFONO, "
				  + " OBSERVACION, "
				  + " ESTADO, "
				  + " USUARIO_CREA, "
				  + " FECHA_CREA "
				  + ") VALUES ("
			  	  + " CCO_LOCAL_INVENTARIO_SEC.nextVal, "
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + PARAMETER
				  + "SYSDATE"
				  + ")";
		public  InsertOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			compile();
		}
		public void execute(LocalInventario localInventario) {
			InsertOperation.this.update( new Object[]{
				localInventario.getIdLocal(),
				localInventario.getAgua(),
				localInventario.getEnergia(),
				localInventario.getTelefono(),
				localInventario.getObservacion(),
				localInventario.getEstado(),
				localInventario.getUsuarioCrea()
				});
		}
    }

    // ------------- UpdateOperation -------------------------------------
    class UpdateOperation extends SqlUpdate	{
    	private static final String sql = "UPDATE CCO_LOCAL_INVENTARIO_T SET "
					  + " ID_LOCAL = ? ,"
					  + " AGUA = ? ,"
					  + " ENERGIA = ? ,"
					  + " TELEFONO = ? ,"
					  + " OBSERVACION = ? ,"
					  + " ESTADO = ? ,"
					  + " USUARIO_MODIFICA = ? ,"
					  + " FECHA_MODIFICA = SYSDATE"
				  	  + WHERE
					  + " ID = ? ";
		public  UpdateOperation(DataSource ds) {
			super(ds,sql);
			//set here required parameters			
			declareParameter( new SqlParameter(Types.INTEGER));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));
			declareParameter( new SqlParameter(Types.CHAR));			
			declareParameter( new SqlParameter(Types.INTEGER));
			compile();
		}
		public void execute(LocalInventario localInventario) {
			UpdateOperation.this.update( new Object[]{
				
				localInventario.getIdLocal(),
				localInventario.getAgua(),
				localInventario.getEnergia(),
				localInventario.getTelefono(),
				localInventario.getObservacion(),
				localInventario.getEstado(),
				localInventario.getUsuarioModifica(),
				localInventario.getId()
				});
		}
    }

    // ------------- DeleteOperation -------------------------------------
    class DeleteOperation extends SqlUpdate	{
		private static final String sql = "DELETE FROM CCO_LOCAL_INVENTARIO_T "
										+ WHERE + "1=1";
		public DeleteOperation(DataSource ds) {
			super(ds,sql);
			compile();
		}
		public void execute(LocalInventario localInventario) {
			this.update( new Object[]{
			});
		}
	}

    // ------------- DeleteByPrimaryKey ----------------------------------
    class DeleteByPrimaryKey extends SqlUpdate	{
		private static final String sql = "DELETE FROM CCO_LOCAL_INVENTARIO_T "
										+ WHERE  + "1=1";
		public DeleteByPrimaryKey(DataSource ds) {
			super(ds,sql);
			compile();
		}	
		public void execute(long id) {
			this.update( new Object[]{
			});
		}	
    }

    // ---- BaseQueryOperation for Mapping ResultSet values --------------
	abstract class BaseQueryOperation extends MappingSqlQuery {
		public BaseQueryOperation(DataSource ds,String sql) {
	   		super(ds,sql);
		}	
		protected Object mapRow(ResultSet rs, int rownum) throws SQLException {
			LocalInventario localInventario = new LocalInventario();
				localInventario.setId(new Long(rs.getLong("ID")));
				localInventario.setIdLocal(new Long(rs.getLong("ID_LOCAL")));
				localInventario.setIdLocalStr(rs.getString("ID_LOCAL"));				
				localInventario.setAgua(rs.getString("AGUA"));
				localInventario.setEnergia((rs.getString("ENERGIA")));
				localInventario.setTelefono((rs.getString("TELEFONO")));				
				localInventario.setObservacion(rs.getString("OBSERVACION"));
				localInventario.setEstado(rs.getString("ESTADO"));
				localInventario.setUsuarioCrea(rs.getString("USUARIO_CREA"));
				localInventario.setFechaCrea(rs.getDate("FECHA_CREA"));		
				localInventario.setUsuarioModifica(rs.getString("USUARIO_MODIFICA"));
				localInventario.setFechaModifica(rs.getDate("FECHA_MODIFICA"));		
				localInventario.setNumeroLocal(rs.getString("NUMERO_LOCAL"));				
			return localInventario;
		}
    }

    // ------------- FindByPrimaryKeyQuery -------------------------------
    class FindByPrimaryKeyQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE  + "1=1";
    	public FindByPrimaryKeyQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public LocalInventario runQuery(long id) {
			List result =  execute( new Object[]{new Long(id)});
			if ( result.size() > 0 ) {
        		return (LocalInventario) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByCriteriaQuery ---------------------------------    
    class FindByCriteriaQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + " 1=1 ";
			//For required criteria elements uncomment required values
			  	  	  					//+ " AND ID = ? "
			  	  	  					//+ " AND ID_LOCAL = ? "
			  	  	  					//+ " AND ID_LOCAL_TIPO_ELEMENTO = ? "
			  	  	  					//+ " AND CANTIDAD = ? "
			  	  	  					//+ " AND OBSERVACION = ? "
			  	  	  					//+ " AND ESTADO = ? "
			  	  	  					//+ " AND USUARIO_CREA = ? "
			  	  	  					//+ " AND FECHA_CREA = ? "
			  	  	  					//+ " AND USUARIO_MODIFICA = ? "
			  	  	  					//+ " AND FECHA_MODIFICA = ? "
										//+ "";
    	public FindByCriteriaQuery(DataSource ds) {
    		super(ds,sql);
    		//For required criteria elements uncomment required values
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.INTEGER) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			//declareParameter( new SqlParameter(Types.CHAR) );
			//declareParameter( new SqlParameter(Types.DATE) );
			compile();
    	}
    	public LocalInventario runQuery(LocalInventario localInventario) {
			List result =  execute( new Object[]{
			//For required criteria elements uncomment required values
				//localInventario.getId(),
				//localInventario.getIdLocal(),
				//localInventario.getIdLocalTipoElemento(),
				//localInventario.getCantidad(),
				//localInventario.getObservacion(),
				//localInventario.getEstado(),
				//localInventario.getUsuarioCrea(),
				//localInventario.getFechaCrea(),
				//localInventario.getUsuarioModifica(),
				//localInventario.getFechaModifica(),
				});
			if ( result.size() > 0 ) {
        		return (LocalInventario) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindAllQuery ----------------------------------------    
    class FindAllQuery extends BaseQueryOperation {
		private static final String sql = "SELECT LI.*,NUMERO_LOCAL FROM CCO_LOCAL_INVENTARIO_T LI INNER JOIN "
										+ " CCO_LOCAL_T LO ON LI.ID_LOCAL=LO.ID"
										+ WHERE + " ID_LOCAL=? ";
    	public FindAllQuery(DataSource ds) {
    		super(ds,sql);
    		declareParameter( new SqlParameter(Types.INTEGER) );
			compile();
    	}
    	public List runQuery(LocalInventario localInventario) {
			List result =  execute( new Object[]{
					localInventario.getIdLocal()
			});
			if ( result.size() > 0 ) {
        		return  result;
        	} else {
        		return null;
        	}
    	}
    }

    // ------------- FindByActiveStateQuery  -----------------------------
    class FindByActiveStateQuery extends BaseQueryOperation {
		private static final String sql = SELECT
										+ WHERE + "1=1";
    	public FindByActiveStateQuery(DataSource ds) {
    		super(ds,sql);
			compile();
    	}
    	public LocalInventario runQuery(LocalInventario localInventario) {
			List result =  execute( new Object[]{
			});
			if ( result.size() > 0 ) {
        		return (LocalInventario) result.get(0);
        	} else {
        		return null;
        	}
    	}
    }

}
